
create tablespace CODES_TABLES
datafile '/u01/oracle/VLDB/codes_tables.dbf'
default storage
  (initial 1M next 1M pctincrease 0 pctfree 2);

create table EMPLOYEE (
EmpNo          NUMBER(10) primary key,
Name           VARCHAR2(40),
DeptNo         NUMBER(2),
Salary         NUMBER(7,2),
Birth_Date     DATE,
Soc_Sec_Num    VARCHAR2(9),
State_Code     CHAR(2),
constraint FK_DeptNO foreign key (DeptNo)
   references DEPT(DeptNo),
constraint FK_StateCode foreign key (State_Code)
   references State(State_Code),
);

create table EMPLOYEE (
EmpNo          NUMBER(10) primary key,
Name           VARCHAR2(40),
DeptNo         NUMBER(2),
Salary         NUMBER(7,2),
Birth_Date     DATE,
Soc_Sec_Num    VARCHAR2(9),
 constraint FK_DeptNO foreign key (DeptNo)
   references DEPT(DeptNo)
)
partition by range (DeptNo)
 (partition PART1   values less than (11)
   tablespace PART1_TS,
  partition PART2   values less than (21)
   tablespace PART2_TS,
  partition PART3   values less than (31)
   tablespace PART3_TS,
  partition PART4   values less than (MAXVALUE)
   tablespace PART4_TS)
;

select *
  from EMPLOYEE (PART2)
 where DeptNo between 11 and 20;

create index EMPLOYEE_DEPTNO
  on EMPLOYEE(DeptNo)
   local
   (partition PART1
     tablespace PART1_NDX_TS,
    partition PART2
     tablespace PART2_NDX_TS,
    partition PART3
     tablespace PART3_NDX_TS,
    partition PART4
     tablespace PART4_NDX_TS);

create index EMPLOYEE_DEPTNO
on EMPLOYEE(DeptNo)
 global partition by range (DeptNo)
 (partition PART1   values less than (11)
   tablespace PART1_NDX_TS,
  partition PART2   values less than (21)
   tablespace PART2_NDX_TS,
  partition PART3   values less than (31)
   tablespace PART3_NDX_TS,
  partition PART4   values less than (MAXVALUE)
   tablespace PART4_NDX_TS)
;

alter table EMPLOYEE
   modify partition PART1
   storage (next 1M pctincrease 0);


alter table EMPLOYEE
  truncate partition PART3
  drop storage;

alter index EMPLOYEE_DEPTNO
rebuild partition PART4
storage (initial 2M next 2M pctincrease 0);

create table EMPLOYEE (
EmpNo          NUMBER(10) primary key,
Name           VARCHAR2(40),
DeptNo         NUMBER(2),
Salary         NUMBER(7,2),
Birth_Date     DATE,
Soc_Sec_Num    VARCHAR2(9),
 constraint FK_DeptNO foreign key (DeptNo)
   references DEPT(DeptNo),
)
partition by hash (DeptNo)
partitions 10;

create table EMPLOYEE (
EmpNo          NUMBER(10) primary key,
Name           VARCHAR2(40),
DeptNo         NUMBER(2),
Salary         NUMBER(7,2),
Birth_Date     DATE,
Soc_Sec_Num    VARCHAR2(9),
 constraint FK_DeptNO foreign key (DeptNo)
   references DEPT(DeptNo)
)
partition by hash (DeptNo)
partitions 2
store in (PART1_TS, PART2_TS);

create table EMPLOYEE (
EmpNo          NUMBER(10) primary key,
Name           VARCHAR2(40),
DeptNo         NUMBER(2),
Salary         NUMBER(7,2),
Birth_Date     DATE,
Soc_Sec_Num    VARCHAR2(9),
 constraint FK_DeptNO foreign key (DeptNo)
   references DEPT(DeptNo)
)
partition by range (DeptNo)
subpartition by hash (Name)
subpartition 10
 (partition PART1   values less than (11)
   tablespace PART1_TS,
  partition PART2   values less than (21)
   tablespace PART2_TS,
  partition PART3   values less than (31)
   tablespace PART3_TS,
  partition PART4   values less than (MAXVALUE)
   tablespace PART4_TS);

create materialized view SALES_MONTH_MV
tablespace AGG_DATA
refresh complete
start with sysdate
next sysdate+1
enable query rewrite
as
select Sales_Month, SUM(Amount)
  from SALES
 group by Sales_Month;

create index STATE_CODE_DESCRIPTION
on STATE(State_Code, Description);

create index STATE_DESCRIPTION_CODE
on STATE(Description, State_Code);


create table STATE (
State_Code      CHAR(2) primary key,
Description     VARCHAR2(25)
)
organization index;

create bitmap index EMPLOYEE$STATE_CODE$BMAP
    on EMPLOYEE(State_Code);

set transaction use rollback segment NAZWA_SEGMENTU;

insert /*+ APPEND */ into SALES_PERIOD_CUST_AGG
select Period_ID, Customer_ID, SUM(Sales)
  from SALES
 group by Period_ID, Customer_ID;

delete from SALES where Customer_ID=12;

execute DELETE_COMMIT('delete from SALES where Customer_ID=12',1000);

execute DELETE_COMMIT('delete from SALES where State_Code = ''NH''',500)

create or replace procedure DELETE_COMMIT
( p_statement in varchar2,
  p_commit_batch_size   in number default 10000)
is
        cid                             integer;
        changed_statement               varchar2(2000);
        finished                        boolean;
        nofrows                         integer;
        lrowid                          rowid;
        rowcnt                          integer;
        errpsn                          integer;
        sqlfcd                          integer;
        errc                            integer;
        errm                            varchar2(2000);
begin
        /* Jeeli rzeczywista instrukcja zawiera klauzul WHERE, wtedy 
trzeba doczy klauzul rownum < n , po ktrej naley 
zastosowa klauzul AND, w przeciwnym razie stosuje si 
klauzul  WHERE  rownum < n  */
          if ( upper(p_statement) like '% WHERE %') then
                changed_statement := p_statement||' AND rownum < '
               ||to_char(p_commit_batch_size + 1);
        else
changed_statement := p_statement||' WHERE rownum < '
||to_char(p_commit_batch_size + 1);
        end if;
        begin
  cid := dbms_sql.open_cursor; -- Otworzy kursor dla zadania
                dbms_sql.parse(cid,changed_statement,dbms_sql.native);
                        -- parsowa kursor.
  rowcnt := dbms_sql.last_row_count;
                      -- zapamita do pniejszego raportowania 
        exception
           when others then
                     errpsn := dbms_sql.last_error_position;
                        -- daje pozycj bdu w zmienionym sql
                        -- usun instrukcj jeeli co si zdarzy
     sqlfcd := dbms_sql.last_sql_function_code;
                        -- kod funkcji mona znale w podrczniku OCI 
                     lrowid := dbms_sql.last_row_id;
                        -- Zachowa wszystkie te wartoci do 
                        -- raportowania o bdach. Jednak one    
                        -- wszystkie s rzeczywicie uyteczne do 
                        -- wykonania niezalenego procesu pakietu 
                        -- dbms_output, ktrego wyniki maj by udane, 
                        -- co nie bdzie moliwe w przypadku wywoania 
                        -- z formularza lub z narzdzia z frontonem.
                     errc := SQLCODE;
                     errm := SQLERRM;
                     dbms_output.put_line('Error '||to_char(errc)||
                           ' Posn '||to_char(errpsn)||
               ' SQL fCode '||to_char(sqlfcd)||
        ' rowid '||rowidtochar(lrowid));
                    raise_application_error(-20000,errm);
                        -- to zapewni wywietlenie przynajmniej 
                        -- komunikatu o bdzie, jeeli co si 
                        -- zdarzy, nawet w narzdziu z frontonem.
        end;
        finished := FALSE;
        while not (finished)
        loop - nadal wykonywa kursor, dopki nic nie zostanie do 
             -- przetwarzania.
                begin
 nofrows := dbms_sql.execute(cid);
                        rowcnt := dbms_sql.last_row_count;
                exception
                        when others then
                            errpsn := dbms_sql.last_error_position;
                            sqlfcd := dbms_sql.last_sql_function_code;
                            lrowid := dbms_sql.last_row_id;
                              errc := SQLCODE;
                              errm := SQLERRM;
                      dbms_output.put_line('Error '||to_char(errc)||
                             ' Posn '||to_char(errpsn)||
                 ' SQL fCode '||to_char(sqlfcd)||
          ' rowid '||rowidtochar(lrowid));
                                raise_application_error(-20000,errm);
                end;
                if nofrows = 0 then
                        finished := TRUE;
                else
                 finished := FALSE;
                end if;
                commit;
        end loop;
        begin
                dbms_sql.close_cursor(cid);
                        -- zamkn kursor do zakoczenia czyszczenia
        exception
                when others then
                        errpsn := dbms_sql.last_error_position;
                        sqlfcd := dbms_sql.last_sql_function_code;
                        lrowid := dbms_sql.last_row_id;
     errc := SQLCODE;
                        errm := SQLERRM;
       dbms_output.put_line('Error '||to_char(errc)||
      ' Posn '||to_char(errpsn)||
                                ' SQL fCode '||to_char(sqlfcd)||
                                ' rowid '||rowidtochar(lrowid));
                        raise_application_error(-20000,errm);
        end;
end;
/


execute DBMS_TTS.TRANSPORT_SET_CHECK('AGG_DATA,AGG_INDEXES','FALSE');

alter tablespace AGG_DATA read only;
alter tablespace AGG_INDEXES read only;

exp TRANSPORT_TABLESPACE=Y TABLESPACES=(AGG_DATA,AGG_INDEXES) CONSTRAINTS=N GRANTS=Y TRIGGERS=N

imp TRANSPORT_TABLESPACE=Y DATAFILES=(agg_data.dbf, agg_indexes.dbf) 

alter tablespace AGG_DATA read write;
alter tablespace AGG_INDEXES read write;

create tablespace CODES_TABLES
datafile '/u01/oracle/VLDB/codes_tables.dbf'
size 10M
extent management local uniform size 256K;

